package com.fjrz.bbxwj.main.host.side

import android.os.Bundle
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ConvertUtils
import com.module.lemlin.custom.SpacingItemDecoration
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.UserAttention
import com.fjrz.bbxwj.main.host.adpter.AttentionAdapter
import com.fjrz.bbxwj.main.notice.MessageChatActivity
import com.fjrz.bbxwj.mode.vm.UserViewModel
import com.module.lemlin.mode.ViewModeFragment
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.module.lemlin.view.LoginAuthPopup
import kotlinx.android.synthetic.main.activity_message_bulletin.*
import kotlinx.android.synthetic.main.fragment_main_attention.*
import kotlinx.android.synthetic.main.fragment_main_attention.mSmartRefreshLayout

class MainAttentionFragment : ViewModeFragment<UserViewModel>() {


    companion object {
        private const val EXTRA_NAME_STATUS = "EXTRA_NAME_STATUS"
        fun newInstance(status: Int): MainAttentionFragment {
            val bundle: Bundle = Bundle().apply {
                putInt(EXTRA_NAME_STATUS, status)
            }
            return MainAttentionFragment()
                .apply { arguments = bundle }
        }
    }

    private val mAttentionAdapter = AttentionAdapter()

    private val mStatus: Int
        get() = arguments?.getInt(EXTRA_NAME_STATUS) ?: 0

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_attention

    override fun initView() {
        rvAttentionBody.apply {
            val decoration = SpacingItemDecoration(
                vGap = ConvertUtils.dp2px((15f)),
                hGap = ConvertUtils.dp2px((15f))
            )
            addItemDecoration(decoration)
        }.adapter = mAttentionAdapter

        mAttentionAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData: UserAttention = mAttentionAdapter.getItem(position)
            if (view.id == R.id.tvAttentionPrivate) {
                MessageChatActivity.open(
                    activity = requireActivity(),
                    title = itemData.nickname,
                    objectUserId = itemData.id
                )
            }
        }

        //下拉刷新监听
        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.userAttention(mStatus)
        }
        //加载更多刷新监听
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.userAttention(mStatus, isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.userAttention(mStatus)
    }

    override fun initViewMode() {
        viewModel.userAttentionLiveData.observe(viewLifecycleOwner, Observer { response ->
            val status: Status = responseStatus1LiveData(response.status, response.throwable)
            if (status != Status.SUCCESS)
                return@Observer
            val statusAttention: Int = response.httpData?.status ?: 0
            val isLoadMore: Boolean = response.httpData?.loadMore ?: false
            val videoDrafts: MutableList<UserAttention> =
                response.httpData?.data?.list ?: mutableListOf()
            mAttentionAdapter.mAttention = statusAttention
            if (videoDrafts.isNullOrEmpty() && mAttentionAdapter.data.isNullOrEmpty()) {
                mAttentionAdapter.setEmptyView(createViewEmptyNull(hint = "暂无数据"))
            }
            if (isLoadMore) {
                mAttentionAdapter.addData(videoDrafts)
            } else {
                mAttentionAdapter.setNewInstance(videoDrafts)
            }
            if (videoDrafts.size < viewModel.limit) {
                mAttentionAdapter.setFooterView(createViewFooter())
                mSmartRefreshLayout.setEnableLoadMore(false)
            } else {
                mAttentionAdapter.removeAllFooterView()
                mSmartRefreshLayout.setEnableLoadMore(true)
            }
        })
    }

    override fun onEventMainThread(any: Any) {
        if (any !is LoginAuthPopup.Action)
            return
        if (any == LoginAuthPopup.Action.ACTION_LOGIN_SUCCESS) {
            initData()
        }
    }


    private fun responseStatus1LiveData(status: Status, throwable: Throwable?): Status {
        if (status == Status.START) {
            mAttentionAdapter.setEmptyView(createViewEmptyLoad())
        } else if (status == Status.FAILURE) {
            mSmartRefreshLayout.finishLoadMore((false))
            mSmartRefreshLayout.finishRefresh((false))
            mAttentionAdapter.setEmptyView(createViewEmptyNull(hint = throwable?.msg ?: "未知错误"))
        } else {
            mSmartRefreshLayout.finishLoadMore((true))
            mSmartRefreshLayout.finishRefresh((true))
        }
        return status
    }

}